package com.example.emergencyattempt01.activity.student_activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.LoginActivity;
import com.example.emergencyattempt01.activity.administrator_activity.Administrator_main_structure;
import com.example.emergencyattempt01.activity.ex.AutoPager;
import com.example.emergencyattempt01.activity.ex.AutoPagerBuilder;
import com.example.emergencyattempt01.fragement.user_fragment.Borrow_Book_Fragment;
import com.example.emergencyattempt01.fragement.user_fragment.Return_Book_Fragment;
import com.example.emergencyattempt01.fragement.user_fragment.Student_Checking_Fragment;
import com.example.emergencyattempt01.user.Student;
import com.example.emergencyattempt01.user.User;
import de.hdodenhof.circleimageview.CircleImageView;

public class Student_Main_Structure extends AppCompatActivity {
    FrameLayout fragment;// 用于展示fragment
    RadioGroup radioGroup;// 用于承装菜单选项
    ImageView quit;

    // 侧拉界面的
    CircleImageView profile;
    TextView userNameField;
    TextView uidField;
    TextView instituteField;
    TextView identityField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_main_structure);
        initUser();
        // 设置自动播放器
        setAutoPlayer();
        // 设置fragment
        setFragment();
        // 设置退出
        setQuit();
        // 设置侧拉界面
        setSlide();
    }

    private void setSlide() {
        profile=findViewById(R.id.side_profile);
        identityField=findViewById(R.id.side_identity);
        uidField=findViewById(R.id.side_uid);
        instituteField=findViewById(R.id.side_institute);
        userNameField=findViewById(R.id.side_name);

        if(Student.getInstance().getUserType().equals("学生"))
        {
            profile.setImageResource(R.drawable.ic_student_profile);
        }
        else
        {
            profile.setImageResource(R.drawable.ic_administrator);
        }

        identityField.setText(Student.getInstance().getIdentity());
        uidField.setText("uid:"+Student.getInstance().getUid());
        userNameField.setText(Student.getInstance().getUserName());
        instituteField.setText(Student.getInstance().getInstitute());


    }

    // 以下部分用于防止中间进入到login界面，同时维持可登录次数
    @Override
    public void onBackPressed() {
        int count= getSharedPreferences("personal_information",MODE_PRIVATE).getInt("loginWithoutCheckCount",0);
        SharedPreferences.Editor editor=getSharedPreferences("personal_information",MODE_PRIVATE).edit();
        editor.putInt("loginWithoutCheckCount",count-1);
        editor.commit();// 切勿忘记更新！！
        Log.i("loginCount_main",count+"");
        Intent home = new Intent(Intent.ACTION_MAIN);
        home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        home.addCategory(Intent.CATEGORY_HOME);
        startActivity(home);
    }
    private void setQuit() {
        quit=findViewById(R.id.student_main_quit);
        quit.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              SharedPreferences sharedPreferences= Student_Main_Structure.this.getSharedPreferences("personal_information",MODE_PRIVATE);
              SharedPreferences.Editor editor=sharedPreferences.edit();
              editor.putInt("loginWithoutCheckCount",0);
              editor.commit();
              Intent intent=new Intent(Student_Main_Structure.this, LoginActivity.class);
              startActivity(intent);
          }
      });
    }

    private void setFragment() {
        fragment=findViewById(R.id.st_fragment);
        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        Fragment f=new Borrow_Book_Fragment();
        ft.replace(R.id.st_fragment,f).commit();
        radioGroup=findViewById(R.id.st_radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentManager fm=getFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                Fragment f=null;
                switch (checkedId)
                {
                    case R.id.borrow_book_fragment:
                        f=new Borrow_Book_Fragment();
                        ft.replace(R.id.st_fragment,f).commit();
                        break;
                    case R.id.student_checking_fragment:
                        f=new Student_Checking_Fragment();
                        ft.replace(R.id.st_fragment,f).commit();
                        break;
                    case R.id.return_book_fragment:
                        f=new Return_Book_Fragment();
                        ft.replace(R.id.st_fragment,f).commit();
                        break;
                }
            }
        });

    }

    private void setAutoPlayer() {
        AutoPagerBuilder autoPagerBuilder=new AutoPagerBuilder(Student_Main_Structure.this);
        AutoPager autoPagerForMain=autoPagerBuilder.build();
        autoPagerForMain.start();
    }

    private void initUser() {
        User student=Student.getInstance();
        Student.initUser(Student_Main_Structure.this,student);
    }
}